package designpattern.proxy;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import util.GsonUtil;

import java.lang.reflect.Method;

/**
 * @Author Antony
 * @Since 2018/6/7 15:00
 */
public class CGlibProxy implements MethodInterceptor {

    private Object proxyObject;
    private String proxyName;

    public CGlibProxy(Object proxyObject) {
        this.proxyObject = proxyObject;
    }

    public CGlibProxy(Object proxyObject, String proxyName) {
        this.proxyObject = proxyObject;
        this.proxyName = proxyName;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("CGLib Proxy --- 调用前 --- 调用方法是:"+method.getName() + "---参数是:"+ GsonUtil.toJson(objects));

        Object ret = method.invoke(proxyObject, objects);

        System.out.println("CGLib Proxy --- 调用后");

        return ret;
    }
}
